using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Appr;
using net_work.IService.Appr;

namespace net_work.Controllers.Appr;

[Route("api/[controller]")]
[ApiController]
public class ApprovalController(IApprovalService approvalService, ILogger<ApprovalController> logger) : ControllerBase
{
    /// <summary>
    /// 获取审批流程带历史记录列表
    /// </summary>
    /// <returns>返回结果</returns>
    [HttpPost("getHistory")]
    [Authorize]
    public async Task<IActionResult> GetListWithHistory(PagedRequest pagedRequest)
    {
        return AjaxResult.Success(await approvalService.GetListWithHistory(pagedRequest));
    }

    /// <summary>
    /// 获取待处理审批流程列表
    /// </summary>
    /// <returns>返回结果</returns>
    [HttpPost("getToDoList")]
    [Authorize]
    public async Task<IActionResult> GetToDoList(PagedRequest pagedRequest)
    {
        return AjaxResult.Success(await approvalService.GetToDoList(pagedRequest));
    }

    /// <summary>
    /// 启动审批流程
    /// </summary>
    /// <param name="approvalInstanceAddRequest">请求参数</param>
    /// <returns>返回结果</returns>
    [HttpPost("start")]
    [Authorize]
    public async Task<IActionResult> Start([FromBody] ApprovalInstanceAddRequest approvalInstanceAddRequest)
    {
        logger.LogInformation("启动审批流程");
        return await approvalService.Start(approvalInstanceAddRequest) > 0 ? AjaxResult.Success() : AjaxResult.Error();
    }

    /// <summary>
    /// 提交审批结果
    /// </summary>
    /// <param name="approvalSubmit">请求参数</param>
    /// <returns>返回结果</returns>
    [HttpPost("submit")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> Submit([FromBody] ApprovalSubmit approvalSubmit)
    {
        logger.LogInformation("提交审批结果");
        return await approvalService.Submit(approvalSubmit) > 0 ? AjaxResult.Success() : AjaxResult.Error();
    }
}